#include "oopless-parser.hpp"
#include "catch.hpp"
#include <iostream>

std::vector<TFace> getfaces(const std::string &test) {
using namespace std;
    stringstream ss;
    ss << test;
    return parse_face<stringstream>(ss);
}

TEST_CASE ("face parsing case: faces with 3 indices", "[Parser]" ) {
    std::string sample = "			0.000000 0.000000 1.000000 1.000000 0.000000 6 0 7 0xff727278 common/materials/stone/stone";
    struct TFace f = getfaces(sample).front();
    REQUIRE (f.m_Indices.size() == 3);
}

TEST_CASE ("face parsing case: faces with 4 indices", "[Parser]") {
    std::string sample = "			0.000000 0.000000 1.000000 1.000000 0.000000 3 1 0 4 0xffffffff common/materials/metal/aluminum";
    struct TFace f = getfaces(sample).front();
    REQUIRE (f.m_Indices.size() == 4);
}

TEST_CASE ("face parsing case: faces with 5 indices", "[Parser]") {
    std::string sample = "			0.000000 0.000000 1.000000 1.000000 0.000000 2 7 5 7 4 0xff727278 common/materials/stone/stone";
    struct TFace f = getfaces(sample).front();
    REQUIRE (f.m_Indices.size() == 5);
}

TEST_CASE ("face parsing case: faces with 9 indices", "[Parser]") {
    std::string sample = "			0.000000 0.000000 1.000000 1.000000 0.000000 2 7 5 7 4 7 5 7 4 0xff727278 common/materials/stone/stone";
    struct TFace f = getfaces(sample).front();
    REQUIRE (f.m_Indices.size() == 9);
}

TEST_CASE("face parsing case: faces with no material", "[Parser]") {
    std::string sample = 
    "			0.000000 0.000000 1.000000 1.000000 0.000000 1 0 4 5 0x00000000 ";
    std::string answer; //empty string
    struct TFace f = getfaces(sample).front();
    REQUIRE (f.m_Material == answer);
}

TEST_CASE("face parsing case: face's material name is preserved", "[Parser]") {
    std::string sample =
    "			0.000000 0.000000 1.000000 1.000000 0.000000 3 1 0 4 0xffffffff common/materials/metal/aluminum";
    std::string answer = "common/materials/metal/aluminum";
    struct TFace f = getfaces(sample).front();
    REQUIRE (f.m_Material == answer);
}

TEST_CASE("face parsing case: face's color code is preserved", "[Parser]") {
    std::string sample = 
    "			0.000000 0.000000 1.000000 1.000000 0.000000 2 4 1 5 0xffa5a5a5 common/materials/metal/p_metal3";
    std::string answer = "0xffa5a5a5";
    struct TFace f = getfaces(sample).front();
    REQUIRE (f.hex == answer);
}

TEST_CASE("brush parser case: returns populated Vertices and Faces list", "[Parser]") {
    std::string sample = " vertices\n\
			-23.000000 -20.000000 15.000000\n\
			9.000000 -20.000000 15.000000\n\
			9.000000 -20.000000 -17.000000\n\
			-15.000000 -24.000000 -9.000000\n\
			-23.000000 -20.000000 -17.000000\n\
			1.000000 -24.000000 7.000000\n\
			1.000000 -24.000000 -9.000000\n\
			-15.000000 -24.000000 7.000000\n\
		faces\n\
			0.000000 0.000000 1.000000 1.000000 0.000000 0 1 2 4 0xff888888 common/materials/metal/p_metal3\n\
			0.000000 0.000000 1.000000 1.000000 0.000000 3 4 2 6 0xff888888 common/materials/metal/p_metal3\n\
			0.000000 0.000000 1.000000 1.000000 0.000000 2 1 5 6 0xff888888 common/materials/metal/p_metal3\n\
			0.000000 0.000000 1.000000 1.000000 0.000000 0 4 3 7 0xff888888 common/materials/metal/p_metal3\n\
			0.000000 0.000000 1.000000 1.000000 131.810318 5 1 0 7 0xff888888 common/materials/metal/p_metal3\n\
			0.000000 0.000000 1.000000 1.000000 0.000000 3 6 5 7 0xff888888 common/materials/metal/p_metal3";
    std::stringstream ss;
    ss << sample;
    struct TBrush b = parse_brush<std::stringstream>(ss);
    REQUIRE ((b.m_Vertices.size() == 8 && b.m_Faces.size() == 6));
}

TEST_CASE("brush parser case: returns empty when given empty input", "[Parser]") {
    std::string empty;
    std::stringstream ss;
    ss << empty;
    struct TBrush b = parse_brush<std::stringstream>(ss);
    REQUIRE ((b.m_Vertices.size() == 0 && b.m_Faces.size() == 0));
}

TEST_CASE("get entity case: recognizes brush entites", "[Parser]") {
    std::string sample = "		type JumpPad\n\
		String32 target jp2\n\
	brush\n\
		vertices\n\
			324.000000 36.000000 -228.000000\n\
			352.000000 36.000000 -228.000000\n\
			352.000000 36.000000 -380.000000\n\
			324.000000 36.000000 -380.000000\n\
			324.000000 32.000000 -228.000000\n\
			352.000000 32.000000 -228.000000\n\
			352.000000 32.000000 -380.000000\n\
			324.000000 32.000000 -380.000000\n\
		faces\n\
			0.000000 0.000000 -0.250000 -0.000000 270.000000 0 1 2 3 0x00000000 \n\
			0.000000 0.000000 -0.250000 -0.000000 0.000000 6 5 4 7 0x00000000 \n\
			0.000000 0.000000 -0.250000 -0.000000 0.000000 2 1 5 6 0x00000000 \n\
			0.000000 0.000000 -0.250000 -0.000000 0.000000 0 3 7 4 0x00000000 \n\
			0.000000 0.000000 -0.250000 -0.000000 0.000000 3 2 6 7 0x00000000 \n\
			0.000000 0.000000 -0.250000 -0.000000 0.000000 1 0 4 5 0x00000000 ";
    std::stringstream ss;
    ss << sample;
    std::vector<std::string> ent = get_entity<std::stringstream>(ss);
    unsigned int count = 0;
    for (const std::string &x : ent) {
        if (x.find(KEYWORD_BRUSH) != std::string::npos) {
            count++;
        }
    }
    REQUIRE ((count == 1 && ent.size() == 19));
}

TEST_CASE("get entity case: omits unaffiliated brush") {
    std::string sample = "		type WorldSpawn\n\
		String32 targetGameOverCamera end\n\
		ColourXRGB32 colorTeamA ff801313\n\
		ColourXRGB32 fogColor ff7f5122\n\
		Float fogDistanceEnd 3500.000000\n\
		String256 title Static Discharge\n\
		UInt8 playersMax 6\n\
	brush\n\
		vertices\n\
			-19.000000 28.000000 23.000000\n\
			29.000000 28.000000 23.000000\n\
			29.000000 28.000000 -25.000000\n\
			-19.000000 28.000000 -25.000000\n\
			-19.000000 -20.000000 23.000000\n\
			29.000000 -20.000000 23.000000\n\
			29.000000 -20.000000 -25.000000\n\
			-19.000000 -20.000000 -25.000000\n\
		faces\n\
			0.000000 0.000000 1.000000 1.000000 0.000000 0 1 2 3 0xff888888 common/materials/metal/p_metal3\n\
			0.000000 0.000000 1.000000 1.000000 0.000000 6 5 4 7 0xff888888 common/materials/metal/p_metal3\n\
			0.000000 0.000000 1.000000 1.000000 0.000000 2 1 5 6 0xff888888 common/materials/metal/p_metal3\n\
			0.000000 0.000000 1.000000 1.000000 0.000000 0 3 7 4 0xff888888 common/materials/metal/p_metal3\n\
			0.000000 0.000000 1.000000 1.000000 0.000000 3 2 6 7 0xff888888 common/materials/metal/p_metal3\n\
			0.000000 0.000000 1.000000 1.000000 0.000000 1 0 4 5 0xff888888 common/materials/metal/p_metal3";
    std::stringstream ss;
    ss << sample;
    std::vector<std::string> ent = get_entity<std::stringstream>(ss);
    unsigned int count = 0;
    for (const std::string &x : ent) {
        if (x.find(KEYWORD_BRUSH) != std::string::npos) {
            count++;
        }
    }
    REQUIRE ((count == 0 && ent.size() == 7));
}

